﻿// News Framework
// (C) Tanzim Saqib. Oct 25, 2010.
// http://TanzimSaqib.com | http://newsfx.codeplex.com

namespace TanzimSaqib.NewsFramework
{
    using System.Windows.Controls;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System;

    public class RssListViewModel : ViewModelBase
    {
        #region Properties

        private string _SelectedFeed = string.Empty;
        private Feed _Feed;

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        private string _FeedTitle;
        public string FeedTitle
        {
            get { return _FeedTitle; }

            private set
            {
                _FeedTitle = value;
                RaisePropertyChanged("FeedTitle");
            }
        }

        public ObservableCollection<ItemViewModel> FeedItems { get; private set; }

        public RssListViewModel()
        {
            if (!IsDataLoaded)
            {
                FeedItems = new ObservableCollection<ItemViewModel>();
            }

            IsDataLoaded = false;
        }

        #endregion

        #region Event handlers

        public void RssList_Load(CommandArgs args)
        {
            if (Fx.Instance.RootPage.NavigationContext.QueryString.TryGetValue("title", out _SelectedFeed))
            {
                var feed = Fx.Instance.Feeds[_SelectedFeed];

                FeedData.LoadFromStorage();
                FeedTitle = feed.Title;

                if (FeedData.Feeds.ContainsKey(feed.URL))
                {
                    _Feed = FeedData.Feeds[feed.URL];

                    if (_Feed.Items != null && _Feed.Items.Count > 0)
                    {
                        _Feed.Items.CopyToObservableCollection(FeedItems);
                    }
                    else
                    {
                        FeedTitle = FxConstants.StatusUpdating;
                    }
                }
                else
                {
                    FeedTitle = FxConstants.StatusUpdating;
                }

                FeedData.Load(feed, (feedResponse) =>
                {
                    if (feedResponse.Error == default(Exception))
                    {
                        _Feed = feedResponse.Item;

                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (_Feed.IsLoaded)
                            {
                                _Feed.Items.CopyToObservableCollection(FeedItems);
                                FeedTitle = feed.Title;
                            }
                            else
                            {
                                FeedTitle = FxConstants.StatusUpdateFailed;
                            }
                        });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            FeedTitle = FxConstants.StatusUpdateFailed;
                            MessageBox.Show(FxConstants.ConnectionError, string.Empty, MessageBoxButton.OK);
                        });
                    }
                });

            }
        }

        public void FeedItems_SelectionChanged(CommandArgs args)
        {
            var listBox = args.Source as ListBox;
            Check.IfNot(listBox.SelectedIndex < 0, 
                () => Fx.Instance.GoToPageInAssembly(
                    FxConstants.LocalNamespace, 
                    FxConstants.RssViewCategoryXaml.FormatWith(Fx.Instance.Feeds[_SelectedFeed].URL, 
                    listBox.SelectedIndex)));
        }

        #endregion
    }
}

